﻿using System;
using System.Diagnostics.CodeAnalysis;

namespace Currency.IServices.Shared.Models
{
    /// <summary>
    /// 返回结果包装类,无返回类型(void,task)
    /// </summary>
    public sealed class ResultMessage
    {
        public ResultMessage()
        {
        }

        public ResultMessage([NotNull] ErrorDetails errorDetails)
        {
            ErrorDetails = errorDetails;
        }

        public bool IsSuccess
        {
            get
            {
                return ErrorDetails == null;
            }
        }

        public ErrorDetails ErrorDetails { get; set; }

        public static implicit operator ResultMessage([NotNull] ErrorDetails errorDetails)
        {
            return new ResultMessage
            {
                ErrorDetails = errorDetails
            };
        }
    }

    /// <summary>
    /// 返回结果包装类,有返回类型
    /// </summary>
    [Serializable]
    public sealed class ResultMessage<TValue>
    {
        public ResultMessage()
        {
        }

        public ResultMessage([NotNull] TValue value)
        {
            Content = value;
        }

        public ResultMessage([NotNull] ErrorDetails errorDetails)
        {
            ErrorDetails = errorDetails;
        }

        public bool IsSuccess
        {
            get
            {
                return ErrorDetails == null && Content != null;
            }
        }

        public TValue Content { get; set; }

        public ErrorDetails ErrorDetails { get; set; }

        public static implicit operator ResultMessage<TValue>(ResultMessage result)
        {
            return new ResultMessage<TValue>
            {
                Content = default
                ,
                ErrorDetails = result.ErrorDetails
            };
        }

        public static implicit operator ResultMessage<TValue>(ErrorDetails errorDetails)
        {
            return new ResultMessage<TValue>
            {
                Content = default
                ,
                ErrorDetails = errorDetails
            };
        }

        public static implicit operator ResultMessage<TValue>(TValue value)
        {
            return new ResultMessage<TValue>(value);
        }
    }
}
